package com.partners.configure;

import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.executable.ExecutableValidator;

/**
 * 验证器Validator
 *
 * 不配的话会使用默认的Validator配置，此配置主要作用在于修改默认的配置
 *
 * @author xwencong@163.com
 * @date 2018/7/3 9:07
 */
@Configuration
public class ValidatorConfig {

    /**
     * 复合类型所用的验证器
     * @return Validator
     */
    @Bean
    public Validator validator(){

        // .failFast( true ) 为设置快速错误模式，默认为false
        ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class )
            .configure()
            .failFast( true )
            .buildValidatorFactory();

        return validatorFactory.getValidator();
    }

    /**
     * 简单类型所用的验证器
     * @return ExecutableValidator
     */
    @Bean
    public ExecutableValidator executableValidator() {
        return validator().forExecutables();
    }




}
